// pages/logs/forgetPassword/index.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    parameter: {
      'navbar': '1',
      'return': '1',
      'title': '重置密码',
      'color': true,
      'class': false
    },
    sizeW: 0,
    sizeH: 0,
    userInfo: {},
    BannerInfo: {},
    navH: app.globalData.navHeight,
    code: '获取验证码',
    isCode: true,
    codeNum: '',
    username: '',
    password: '',
    newPassword: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  /* 获取手机号 */
  enterPhone(e) {
    this.setData({
      username: e.detail.value
    })
  },
  /* 输入验证码 */
  enterCode(e) {
    this.setData({
      codeNum: e.detail.value
    })
  },
  /* 输入密码 */
  enterPassword(e) {
    this.setData({
      password: e.detail.value
    })
  },
  /* 输入新密码 */
  enterNewPassword(e) {
    this.setData({
      newPassword: e.detail.value
    })
  },
  /* 
    获取验证码
  */
  getCode() {
    let timer = null;
    let that = this;
    let code = 300;
    let data = {
      codeType: 1,
      phone: this.data.username
    }
    app.basePost('https://mall.localhost.com/factory/sysuser/getCode', data, res => {
      if (res.code == 200) {
        if (that.data.isCode) {
          clearInterval(timer)
          timer = setInterval(function () {
            code--;
            if (that.data.code == '1s') {
              clearInterval(timer)
              that.setData({
                code: '获取验证码',
                isCode: true
              })
            } else {
              that.setData({
                code: code + 's',
                isCode: false
              })
            }
          }, 1000)
        }
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /* 
    确认
  */
  reset() {
    var data = {
      code: this.data.codeNum,
      newPassword: this.data.newPassword,
      userName: this.data.username
    }
    if (this.data.phone == '') {
      wx.showToast({
        title: "请输入手机号",
        icon: 'none',
        duration: 1000,
        mask: true,
      })
    } else if (this.data.codeNum == '') {
      wx.showToast({
        title: '请输入验证码',
        icon: 'none',
        duration: 1000,
        mask: true,
      })
    } else {
      if (this.data.password == this.data.newPassword) {
        app.basePost('https://mall.localhost.com/factory/sysuser/resetPass', data, res => {
          if (res.code == 200) {
            wx.showToast({
              title: '密码重置成功',
              icon: 'none',
              duration: 2000,
              mask: true,
            })
            setTimeout(function(){
              wx.navigateTo({
                url: "/pages/logs/logs",
              })
            }, 1000)
          }
        });
      }else {
        wx.showToast({
          title: '两次密码不一致，请重新输入',
          icon: 'none',
          duration: 2000,
          mask: true,
        })
      }

    }
  },
  /* 
    返回上一页
  */
  back() {
    wx.navigateBack({
      delta: 1
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})